/**
 * 
 */
package org.ChrisRiffle.Acme.AcmeTraverser;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * AbstractActionPerformer
 * 
 * Abstract, but provides a little common IO funtionality
 * for saving output to disk
 * 
 * @see ActionPerformer
 * 
 * @author RIF
 *
 */
public abstract class AbstractActionPerformer implements ActionPerformer {

	
	private File fileToSaveResults;
	
	private BufferedWriter outputBufferedWriter;
	
	
	
	/**
	 * Gets a file reference at the jvm system user dir property,
	 * for savign the processed results
	 * 
	 * @return
	 * 		FileToSaveCsvOutput
	 * @throws IOException 
	 */
	protected File getOutputFile() throws IOException {

		if (fileToSaveResults == null) {

			String userDirPath = System.getProperty("user.dir");

			fileToSaveResults = new File(userDirPath + "/" + getOutputFileName());						
			
			//start from a clean new file
			if(!fileToSaveResults.exists()){
				
				fileToSaveResults.createNewFile();
				
			}else{
				
				fileToSaveResults.delete();
				fileToSaveResults.createNewFile();
			}
			
		}
		return fileToSaveResults;
	}
	
	
	/**
	 * Gets the BufferedWriter for IO
	 * 
	 * @return
	 * 		the BufferedWriter for IO
	 * 
	 * @throws IOException
	 * 				If unable to get the BufferedWriter for IO
	 */
	protected BufferedWriter getBufferedWriter() throws IOException {

		if (outputBufferedWriter == null) {

			File outputFile = getOutputFile(); 
				
			outputBufferedWriter = new BufferedWriter(new FileWriter(outputFile));
		}
		return outputBufferedWriter;

	}
	
	
}
